import Vuex from 'vuex'
import equipType from '@/net/equipType'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    year: new Date().getFullYear(),
    userAdcd: '',
    energyType: 0,
    gridHead: [
      { name: 'XZQHMC', value: '行政区划'}
    ],
    gridData: [],
    energyTypeData: [
      { name: '能源类型', value: 0},
      { name: '设备类型', value: 1}
    ],
    sort: 1,
    sortData: [
      { name: '开工总数', value: 1 },
      { name: '确户数', value: 2 },
      { name: '安装完毕', value: 3 }
    ],
    energy: ''
  },
  mutations: {
    changeYear (state, value) {
      state.year = value
    },
    changeUserAdcd (state, value) {
      state.userAdcd = value
    },
    changeEnergyType (state, value) {
      state.energyType = value
    },
    changeGridHead (state, value) {
      state.gridHead = value
    },
    changeGridData (state, value) {
      state.gridData = value
    },
    changeSort (state, value) {
      state.sort = value
    },
    changeEnergy (state, value) {
      state.energy = value
    }
  },
  actions: {
    getLedger ({commit, state}, vm) {
      vm.$notice.loading.show()
      equipType.getEquipType({
        nd: state.year,
        quyxzqhdm: state.userAdcd,
        types: '',
        sorts: state.sort,
        nylxdm: state.energy
      }).then(res => {
        let arrHead = [
          { name: 'XZQHMC', value: '行政区划'},
          { name: 'JHSL', value: '确户数'},
          { name: 'startWork', value: '开工总数(户/台)'},
          { name: 'uninstall', value: '未安装(户/台)'},
          { name: 'underConstruction', value: '施工中(户/台)'},
          { name: 'install', value: '安装完毕(户/台)'}
        ]
        if (state.energyType === 0) {
          res.tipsData1.forEach((value, index) => {
            arrHead.push({
              name: 'ynfs' + index,
              value: value.YNFSDLMC + '(户/台)'
            }) 
          })
          res.datalist.forEach(data => {
            res.tipsData1.forEach((value, index) => {
              data['ynfs' + index] = data['NHSLYNFSDL' + value.YNFSDL] + '/' + data['SBSLYNFSDL' + value.YNFSDL]
            })
            data.startWork = data.NHSLSUM + '/' + data.SBSLSUM
            data.uninstall = data.NHSLSBZT11 + '/' + data.SBSLSBZT11
            data.underConstruction = data.NHSLSBZT12 + '/' + data.SBSLSBZT12
            data.install = data.NHSLSBZT13 + '/' + data.SBSLSBZT13
          })
        } else {
          res.tipsData2.forEach((value, index) => {
            arrHead.push({
              name: 'gnfs' + index,
              value: value.SBLXMC + '(户/台)'
            })
          })
          res.datalist.forEach(data => {
            res.tipsData2.forEach((value, index) => {
              data['gnfs' + index] = data['NHSLSBLX' + value.SBLX] + '/' + data['SBSLSBLX' + value.SBLX]
            })
            data.startWork = data.NHSLSUM + '/' + data.SBSLSUM
            data.uninstall = data.NHSLSBZT11 + '/' + data.SBSLSBZT11
            data.underConstruction = data.NHSLSBZT12 + '/' + data.SBSLSBZT12
            data.install = data.NHSLSBZT13 + '/' + data.SBSLSBZT13
          })
        }
        commit('changeGridHead', arrHead)
        commit('changeGridData', res.datalist)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    }
  }
})
